﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicJournal
{
    public partial class Journals : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["id"] == null)
            {
                if (Request.Params["file"] == null)
                {
                    List<int> years = new DB.DbJournals().GetYears();
                    string html = "<b style=\"font-size: 20px\">Номера журнала</b>";
                    foreach (int year in years)
                    {
                        List<DB.Item.ItemJournal> journals = new DB.DbJournals().GetJournalInYear(year);
                        string htmlString = "<div><table class=\"issue\" cellspacing=\"1\" cellpadding=\"0\"><tbody></tbody></table>";
                        htmlString = htmlString.Insert(htmlString.LastIndexOf("</tbody>"), String.Format("<tr><td style=\"border-right: #aaa 1px solid\">{0}<br><span>год</span></td></tr>", year));
                        for (int i = 0; i < journals.Count; i++)
                        {
                            htmlString = htmlString.Insert(htmlString.LastIndexOf("</tr>"), String.Format("<td onmouseover=\"selectCell(this)\" onmouseout=\"unselectCell(this)\" onclick=\"location.href=\'Journals.aspx?id={0}\'\"><a href=\"Journals.aspx?id={0}\">№ {1}</a></td>", journals[i].Id, journals[i].Number));
                        }
                        html += htmlString;
                    }
                    ContentLabel.Text = html;
                    NavigationLabel.Text = "";
                }
                else
                {
                    DB.Item.ItemJournal item = new DB.DbJournals().GetJournalById(Request.Params["file"].ToString());
                    HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STEAM";
                    if (item.FileName.Length > 0)
                    {
                        string Header = "Attachment; Filename=" + item.FileName;
                        HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
                        System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("\\Account\\files\\journals\\") + item.FileName);
                        HttpContext.Current.Response.WriteFile(file.FullName);
                        HttpContext.Current.Response.End();
                    }
                    Response.Redirect(String.Format("Journals.aspx?id={0}", Request.Params["file"].ToString()));
                }
            }
            else
            {
                List<DB.Item.ItemPublication> publications = new DB.DbPublications().GetPublicationsInJournal(Request.Params["id"].ToString());
                DB.Item.ItemJournal journal = new DB.DbJournals().GetJournalById(Request.Params["id"].ToString());
                string html = String.Format("<table><tbody><tr><td><a href=\"Journals.aspx?file={0}\"><b style=\"font-size: 20px\">№ {1} за {2} год</b></a></td></tr></tbody></table>", journal.Id, journal.Number, journal.Year);
                foreach (DB.Item.ItemPublication publication in publications)
                {
                    html = html.Insert(html.LastIndexOf("</tbody>"), "<tr><td></td></tr>");
                    html = html.Insert(html.LastIndexOf("</td>"), String.Format("<div width=\"500px\" style=\"border-radius: 8px;\" class=\"radius\" bgcolor=\"#009999\"><div><b>{0}</b></div><div>{1}</div><div><a href=\"Abstract.aspx?rus={2}\">Резюме</a>|<a href=\"Abstract.aspx?eng={2}\">Abstract</a>|<a href=\"Abstract.aspx?file={2}\">Скачать</a></div></div>", publication.TitleRussian, new DB.DbPublications().GetPublicationAuthors(publication.Id), publication.Id));
                }
                ContentLabel.Text = html;
                NavigationLabel.Text = "<a href=\"Journals.aspx\">Номера журналов</a>";
            }
        }
    }
}